use std::{error::Error, str::FromStr};

use serde::{Deserialize, Serialize};
use crate::sql::{Select, Table, Value};


#[derive(Debug, Serialize, Deserialize)]
pub enum Statement {
    Create(Table),
    Select(Select),
    Insert(Insert),
    Update(Update),
    Delete,
    Alter,
}

#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct Update {
    pub table_name: String,
    pub primary_key: Option<usize>,
    pub columns: Vec<Entry>,
    pub uwhere: Option<Vec<Entry>>,
}


#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct Insert {
    pub table_name: String,
    pub primary_key: usize,
    pub values: Vec<Vec<Entry>>,
}

#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct Entry {
    pub key: String,
    pub value: Value,
}

impl Into<String> for Entry {

    fn into(self) -> String {
        match self.value {
            Value::Bool(bool) => format!("{}", bool),
            Value::Float(f) => format!("{}", f),
            Value::Int(i) => format!("{}", i),
            Value::Null => format!(""),
            Value::Text(str) => str,
            Value::TimeStamp(i) => format!("{}", i), 
        }
    }
}


impl From<&Entry> for String {
    fn from(entry: &Entry) -> Self {
        match &entry.value {
            Value::Bool(bool) => format!("{}", bool),
            Value::Float(f) => format!("{}", f),
            Value::Int(i) => format!("{}", i),
            Value::Null => format!(""),
            Value::Text(str) => format!("{}", str),
            Value::TimeStamp(i) => format!("{}", i), 
        }
    }
}
